package com.xiehospital.system.service.impl;

import java.util.List;

import com.xiehospital.common.exception.ServiceException;
import com.xiehospital.system.mapper.SysBedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xiehospital.system.mapper.SysReserveMapper;
import com.xiehospital.common.core.domain.entity.SysReserve;
import com.xiehospital.system.service.ISysReserveService;

/**
 * 预约信息Service业务层处理
 *
 * @author xierujian
 * @date 2024-02-27
 */
@Service
public class SysReserveServiceImpl implements ISysReserveService
{
    @Autowired
    private SysReserveMapper sysReserveMapper;
    @Autowired
    private SysBedMapper sysBedMapper;


    /**
     * 查询预约信息
     *
     * @param reserveId 预约信息主键
     * @return 预约信息
     */
    @Override
    public SysReserve selectSysReserveByReserveId(Long reserveId)
    {
        return sysReserveMapper.selectSysReserveByReserveId(reserveId);
    }

    /**
     * 查询预约信息列表
     *
     * @param sysReserve 预约信息
     * @return 预约信息
     */
    @Override
    public List<SysReserve> selectSysReserveList(SysReserve sysReserve)
    {
        return sysReserveMapper.selectSysReserveList(sysReserve);
    }

    /**
     * 新增预约信息
     *
     * @param sysReserve 预约信息
     * @return 结果
     */
    @Override
    public int insertSysReserve(SysReserve sysReserve)
    {
        Long bedId=sysReserve.getBedId();
        sysReserveMapper.reserveToBed(bedId);
        return sysReserveMapper.insertSysReserve(sysReserve);
    }

    /**
     * 修改预约信息
     *
     * @param sysReserve 预约信息
     * @return 结果
     */
    @Override
    public int updateSysReserve(SysReserve sysReserve)
    {
        return sysReserveMapper.updateSysReserve(sysReserve);
    }

    /**
     * 入住操作
     *
     * @param sysReserve 床位信息主键
     * @return 结果
     */
    @Override
    public int checkInSysBed(SysReserve sysReserve)
    {

        //检查当前床位是否有人，patient_id not null则入住失败
        Long bedId=sysReserve.getBedId();
        int count=sysBedMapper.checkPatient(bedId);
        System.out.println(count);
        if(count>1){
            throw new ServiceException("该床位正在使用中，入住失败，请确认信息");
        }else{
            //若当前床位没人，则修改status为1，表示占用，然后把预约表中的患者信息写入床位表。
            sysBedMapper.checkInSysBed(sysReserve);
            //更新患者表信息
            sysBedMapper.checkInSysPatient(sysReserve);
            //接着把sys_reserve的checkedIN修改为1表示已入住
            return sysBedMapper.checkInSysReserve(sysReserve);
        }
    }

    /**
     * 批量删除预约信息
     *
     * @param reserveIds 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteSysReserveByReserveIds(Long[] reserveIds)
    {

        return sysReserveMapper.deleteSysReserveByReserveIds(reserveIds);
    }

    /**
     * 删除预约信息信息
     *
     * @param reserveId 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteSysReserveByReserveId(Long reserveId)
    {
        Long bedId=sysReserveMapper.selectSysReserveByReserveId(reserveId).getBedId();
        sysReserveMapper.cancelReserve(bedId);
        return sysReserveMapper.deleteSysReserveByReserveId(reserveId);
    }
}
